﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using SGP;
namespace Animation_tools
{
    public partial class Form1 : Form
    {
        Animation obj;
        bool looping;
        bool mousedown;
        int imgid;
        string name;
        bool ap;
        bool srect;
        CSGP_Direct3D DX;
        CSGP_TextureManager TM;
        Rectangle rect;
        Point p;
       List<Animation.Frame> frames;
        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }
        public Form1()
        {
            frames = new List<Animation.Frame>();
            mousedown = false;
            srect = true;
            ap = false;
            name = "Reload.png";
            InitializeComponent();
            DX = CSGP_Direct3D.GetInstance();
            TM = CSGP_TextureManager.GetInstance();
            obj = new Animation();
            DX.Initialize(panel1, false);
            DX.Initialize(panel2, false);
            DX.AddRenderTarget(panel2);
            TM.Initialize(DX.Device, DX.Sprite);
            imgid = TM.LoadTexture("resource/" + name);
            obj.Initialize("resource/Animationtest.xml");
            groupBox2.Visible = false;
            textBox2.Text = name;
            numericX.Value = obj.Frames[obj.animation.CurrFrame].Anchorpoint.X;
            numericY.Value = obj.Frames[obj.animation.CurrFrame].Anchorpoint.Y;
            numericTop.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Top;
            numericBottom.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Bottom;
            numericLeft.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Left;
            numericRight.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Right;
            comboBox1.Text = obj.Frames[obj.animation.CurrFrame].trigger;
            numericDuration.Value = (decimal)obj.Frames[obj.animation.CurrFrame].fDuration;
            for (int i = 0; i < obj.Frames.Count; i++)
            {
                Animation.Frame frame = new Animation.Frame();
                frame.Anchorpoint = obj.Frames[i].Anchorpoint;
                frame.fDuration = obj.Frames[i].fDuration;
                frame.SrcRect = obj.Frames[i].SrcRect;
                frame.trigger = obj.Frames[i].trigger;
                listBox1.Items.Add(frame);
            }
        }

        public new void Update()
        {
            obj.Update(0.001f);
        }

        public void Render()
        {
            DX.Clear(panel1, Color.Cyan);
            DX.DeviceBegin();
            DX.SpriteBegin();
            TM.Draw(imgid, -hScrollBar1.Value, -vScrollBar1.Value);
            DX.DrawHollowRect(rect,Color.Crimson,1);
            Size s = new Size(5, 5);
            Rectangle r = new Rectangle(p, s);
            DX.DrawRect(r, Color.Chocolate);
            DX.SpriteEnd();
            DX.DeviceEnd();
            DX.Present();

            DX.Clear(panel2, Color.Black);
            DX.DeviceBegin();
            DX.SpriteBegin();
            obj.Render(panel2.ClientSize.Width/2, panel2.ClientSize.Height/2, false, 1.0f,Color.AntiqueWhite);
            DX.SpriteEnd();
            DX.DeviceEnd();
            DX.Present();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            looping = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void previewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = !groupBox2.Visible;
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            obj.Frames.Clear();
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                obj.Initialize(dlg.FileName);
                name = obj.rst;
                imgid = obj.TextureID;
                textBox2.Text = name;
                numericX.Value = obj.Frames[obj.animation.CurrFrame].Anchorpoint.X;
                numericY.Value = obj.Frames[obj.animation.CurrFrame].Anchorpoint.Y;
                numericTop.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Top;
                numericBottom.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Bottom;
                numericLeft.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Left;
                numericRight.Value = obj.Frames[obj.animation.CurrFrame].SrcRect.Right;
                comboBox1.Text = obj.Frames[obj.animation.CurrFrame].trigger;
                numericDuration.Value = (decimal)obj.Frames[obj.animation.CurrFrame].fDuration;
                for (int i = 0; i < obj.Frames.Count; i++)
                {
                    Animation.Frame frame = new Animation.Frame();
                    frame.Anchorpoint = obj.Frames[i].Anchorpoint;
                    frame.fDuration = obj.Frames[i].fDuration;
                    frame.SrcRect = obj.Frames[i].SrcRect;
                    frame.trigger = obj.Frames[i].trigger;
                    listBox1.Items.Add(frame);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            obj.Playing = !obj.Playing;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement Root = new XElement("Animation");
                XAttribute XName = new XAttribute("Name", name);
                Root.Add(XName);
                XElement XFrames = new XElement("Frames");
                Root.Add(XFrames);
                for (int i = 0; i < obj.Frames.Count; i++)
                {
                    XElement XFrame = new XElement("Frame");
                    XFrames.Add(XFrame);
                    XAttribute XTop = new XAttribute("Top", obj.Frames[i].SrcRect.Top);
                    XFrame.Add(XTop);

                    XAttribute XBottom = new XAttribute("Bottom", obj.Frames[i].SrcRect.Bottom);
                    XFrame.Add(XBottom);

                    XAttribute XLeft = new XAttribute("Left", obj.Frames[i].SrcRect.Left);
                    XFrame.Add(XLeft);

                    XAttribute XRight = new XAttribute("Right", obj.Frames[i].SrcRect.Right);
                    XFrame.Add(XRight);

                    XAttribute X = new XAttribute("X", obj.Frames[i].Anchorpoint.X);
                    XFrame.Add(X);

                    XAttribute Y = new XAttribute("Y", obj.Frames[i].Anchorpoint.Y);
                    XFrame.Add(Y);

                    XAttribute XDuration = new XAttribute("Duration", obj.Frames[i].fDuration);
                    XFrame.Add(XDuration);

                    XAttribute XTrigger = new XAttribute("Trigger", obj.Frames[i].trigger);
                    XFrame.Add(XTrigger);
                }
                Root.Save(dlg.FileName);
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (mousedown == false && srect == true)
            {
                mousedown = true;
                rect = new Rectangle(e.Location.X - hScrollBar1.Value, e.Location.Y - vScrollBar1.Value,0,0);
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {

            //Rectangle's width = mouse's x - rectangle's x (if rectangle's x is NOT bigger than mouse's x)
            //Rectangle's height = mouse's y - rectangle's y (if rectangle's y is NOT bigger than mouse's y)
            if(mousedown == true && srect == true)
            {
                if (rect.Width <= 50)
                    rect.Width += 1;
                
                if (rect.Height <= 50)
                    rect.Height += 1;

                if (rect.X < e.Location.X)
                    rect.Width = e.Location.X - rect.X;

                if (rect.Y < e.Location.Y)
                    rect.Height = e.Location.Y - rect.Y;

                numericLeft.Value = rect.Left;

                numericBottom.Value = rect.Bottom;

                numericRight.Value = rect.Right;

                numericTop.Value = rect.Top;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if(srect == true)
            mousedown = false;
        }

        private void numericDuration_ValueChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].fDuration = (double)numericDuration.Value;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].trigger = comboBox1.Text;
        }

        private void numericX_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].Anchorpoint.X = (int)numericX.Value;
        }

        private void numericY_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].Anchorpoint.Y = (int)numericY.Value;
        }

        private void numericLeft_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].SrcRect.Left = (int)numericLeft.Value;
        }

        private void numericTop_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].SrcRect.Top = (int)numericTop.Value;
        }

        private void numericRight_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].SrcRect.Right = (int)numericRight.Value;
        }

        private void numericBottom_ValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            obj.Frames[listBox1.SelectedIndex].SrcRect.Bottom = (int)numericBottom.Value;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Animation.Frame frame;
            if (listBox1.SelectedIndex > -1)
            {
                frame = (Animation.Frame)listBox1.Items[listBox1.SelectedIndex];
                numericDuration.Value = (decimal)frame.fDuration;
                numericBottom.Value = frame.SrcRect.Bottom;
                numericTop.Value = frame.SrcRect.Top;
                numericRight.Value = frame.SrcRect.Right;
                numericLeft.Value = frame.SrcRect.Left;
                numericX.Value = frame.Anchorpoint.X;
                numericY.Value = frame.Anchorpoint.Y;
                comboBox1.Text = frame.trigger;
                p = frame.Anchorpoint;
                rect.Height = frame.SrcRect.Bottom - frame.SrcRect.Top;
                rect.Width = frame.SrcRect.Right - frame.SrcRect.Left;
                rect.X = frame.Anchorpoint.X - rect.Width/2;
                rect.Y = frame.Anchorpoint.Y - rect.Height/2;
            }
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Animation.Frame frame = new Animation.Frame();
            frame.SrcRect.Left = (int)numericLeft.Value;
            frame.SrcRect.Right = (int)numericRight.Value;
            frame.SrcRect.Bottom = (int)numericBottom.Value;
            frame.SrcRect.Top = (int)numericTop.Value;
            frame.Anchorpoint.X = (int)numericX.Value;
            frame.Anchorpoint.Y = (int)numericY.Value;
            frame.fDuration = (double)numericDuration.Value;
            frame.trigger = comboBox1.Text;
            obj.Frames.Add(frame);
            listBox1.Items.Add(frame);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (ap == true)
            {
                p = e.Location;
                numericX.Value = p.X;
                numericY.Value = p.Y;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ap = false;
            srect = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ap = true;
            srect = false;
        }
    }
}
